const getComment = require('./getComment')
const getDownloadCount = require('./getDownloadCount')

module.exports = function getInfo (driver, idleIndex, taskDispatcher) {
  let taskInfo = taskDispatcher.getTaskInfo(idleIndex)
  let appid = taskInfo.appid
  return getComment(driver, appid)
  .then((comment) => {
    Object.assign(taskInfo, comment)
    console.info(`${taskInfo.appname} 评分抓取完成`)
    return getDownloadCount(driver, appid)
  })
  .then((download) => {
    Object.assign(taskInfo, download)
    console.info(`${taskInfo.appname} 下载量抓取完成`)
    const nextIdle = taskDispatcher.dispatch()
    if (nextIdle === -1) {
      driver.quit()
      return 
    }
    return getInfo(driver, nextIdle, taskDispatcher)
  })
  .catch(() => {
    driver.quit()
  })
}